package com.example.demo.service;

import cn.hutool.core.util.ArrayUtil;
import com.example.demo.repository.ImportExcelmapper;
import com.example.demo.utils.Result;
import com.example.demo.utils.SystemCodeEnum;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @Description TODO
 * @Author zhaoHeng
 * @Date 2021/12/31 0031 10:15
 * @Version 1.0
 */
@Service
public class ImportExcelService {
    @Resource
    private ImportExcelmapper importExcelmapper;

    public Result upload(HSSFWorkbook wb, String filename) {
        for (int i = 0; i < wb.getNumberOfSheets(); i++) {
            HSSFSheet sheetAt = wb.getSheetAt(i);
            String sheetName = wb.getSheetName(i) + filename;
            ArrayList<String> create = create(sheetAt);
            try {
                importExcelmapper.createTable(sheetName, create);

            } catch (Exception e) {
                return Result.error(SystemCodeEnum.FAILEDTOCREATEADATATABLE);
            }
            try {
                insert(sheetAt, sheetName);
            } catch (Exception e) {
                return Result.error(SystemCodeEnum.INSERTFAILED);
            }
        }
        return Result.ok(SystemCodeEnum.SYSTEM_OK);
    }

    private static String getValue(HSSFCell hssfCell) {
        if (hssfCell.getCellType() == CellType.BOOLEAN) {
            return String.valueOf(hssfCell.getBooleanCellValue());
        } else if (hssfCell.getCellType() == CellType.NUMERIC) {
            return String.valueOf(hssfCell.getNumericCellValue());
        } else {
            return String.valueOf(hssfCell.getStringCellValue());
        }
    }

    private static ArrayList<String> create(HSSFSheet hssfSheet) {
        ArrayList<String> list = new ArrayList<>();
        //遍历行row
        for (int rownum = 0; rownum <= hssfSheet.getLastRowNum(); rownum++) {
            HSSFRow sheetRow = hssfSheet.getRow(rownum);
            if (sheetRow == null) {
                continue;
            }
            //遍历列cell
            for (int cellnum = 0; cellnum <= sheetRow.getLastCellNum(); cellnum++) {
                HSSFCell cell = sheetRow.getCell(cellnum);
                if (cell == null) {
                    continue;
                }
                if (rownum == 0) {
                    list.add(getValue(cell));
                }
            }
        }
        return list;
    }

    private ArrayList<String> insert(HSSFSheet hssfSheet, String sheetName) {
        ArrayList<String> insert = new ArrayList<>();
        ArrayList<String> values = new ArrayList<>();

        //遍历行row
        for (int rownum = 0; rownum <= hssfSheet.getLastRowNum(); rownum++) {
            HSSFRow sheetRow = hssfSheet.getRow(rownum);
            if (values.size() != 0) {
                values.clear();
            }
            if (sheetRow == null) {
                continue;
            }


            //遍历列cell
            for (int cellnum = 0; cellnum <= sheetRow.getLastCellNum(); cellnum++) {
                HSSFCell cell = sheetRow.getCell(cellnum);
                if (cell == null) {
                    continue;
                }
                if (rownum == 0) {
                    insert.add(getValue(cell));
                }
                if (rownum != 0) {
                    values.add(getValue(cell));
                }
            }
            String[] strings = new String[values.size()];
            String[] array = values.toArray(strings);
            if (array.length == insert.size()) {
                importExcelmapper.insertTable(sheetName, insert, array);
            }

        }
        //int lastRowNum = hssfSheet.getLastRowNum();
        //
        //String[] strings = new String[insert.size()];
        //String[] insertArr = insert.toArray(strings);
        //for (int i = 0; i < lastRowNum; i++) {
        //    Arrays.copyOf(insertArr,)
        //}
        return insert;
    }
}


